package server.server02;

import server.server02.Request;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 加入Servlet,整合配置文件解耦业务代码
 */
public class Server06 {
    private ServerSocket serverCocket;
    public static void main(String[] args) {
        Server06 server = new Server06();
        server.start();
    }
//启动服务
    public void start(){
        try {
            serverCocket = new ServerSocket(8777);
            receive();
        } catch (IOException e) {
            System.out.println("服务器启动失败");
             e.printStackTrace();
        }
    }
//接受连接
    public void receive(){
        try {
            System.out.println("一个客户端建立了连接");
                Socket client =  serverCocket.accept();
                //获取请求协议
                 Request request =  new Request(client);
                //响应
                Response response = new Response(client);
                Servlet servlet = WebApp.getServletFromeUrl(request.getUrl());
                if(null!=servlet){
                    servlet.service(request,response);
                    response.pushBrower(200);
                }else {
                    //错误
                    response.pushBrower(404);
                }

        } catch (IOException e) {
            System.out.println("客户端错误");
            e.printStackTrace();
        }
    }
//停止服务
    public void stop(){

    }
}
